package cn.faster.common.utils.sys.shiro;

import cn.faster.sys.entity.SysUser;
import cn.faster.sys.service.SysUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xiaoCoder
 * @version 1.0
 * @description: 用户登录之后将数据存储到 ShiroSecurityUtils 中，手动进行更新登录用户的信息
 * @date 2021/8/17 0:33
 */
@Component
@Log4j2
public class ShiroSecurityUtils {

    private interface ConstEnum {

        String UPDATE_GLOBAL_NON_STORAGE = "00A";

        String UPDATE_GLOBAL_AND_STORAGE = "00B";

    }

    private static SysUser sysUser;

    @Autowired
    private SysUserService sysUserService;

    /* *
     * @description: 登录成功之后进行全局存储
     * @author xiaoCoder
     * @date: 2021/8/17 0:41
     * @param: []
     * @return: cn.faster.sys.entity.SysUser
     */
    public static SysUser getLoginSysUserInfo() {
        synchronized (ShiroSecurityUtils.class) {
            if (sysUser == null) {
                sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            }
            return sysUser;
        }
    }

    /* *
     * @description: 设置登录之后的SysUser信息
     * @author xiaoCoder
     * @date: 2021/8/17 1:11
     * @param: [sysUser]
     * @return: void
     */
    public static void setLoginSysUserInfo(SysUser sysUser) throws Exception {
        synchronized (ShiroSecurityUtils.class) {
            log.info(">>>ShiroSecurityUtils>>>setLoginSysUserInfo>>>start 设置 sysUser：{}", JSONObject.toJSONString(sysUser));
            if (sysUser == null) {
                throw new NullPointerException("sysUser为空！");
            }
            ShiroSecurityUtils.sysUser = sysUser;
            log.info(">>>ShiroSecurityUtils>>>setLoginSysUserInfo>>>success 设置 sysUser：{}", JSONObject.toJSONString(ShiroSecurityUtils.sysUser));
        }
    }

    /* *
     * @description: 修改用户全局状态信息
     * @author xiaoCoder
     * @date: 2021/8/17 0:42
     * @param: [sysUser,constEnum]
     * @return: cn.faster.sys.entity.SysUser
     */
    public SysUser updateSysUserInfo(SysUser sysUser, String constEnum) {
        synchronized (ShiroSecurityUtils.class) {
            log.info(">>>使用单例开始更新全局用户信息>>>");
            if (sysUser == null) {
                return ShiroSecurityUtils.sysUser;
            } else {
                ShiroSecurityUtils.sysUser = sysUser;
            }
            if (constEnum.equals(ConstEnum.UPDATE_GLOBAL_AND_STORAGE)) {
                log.info(">>>ShiroSecurityUtils>>>updateSysUserInfo>>>进行入库操作");
                boolean flag = sysUserService.update(sysUser, new UpdateWrapper<SysUser>().eq("id", sysUser.getId()));
                log.info(">>>ShiroSecurityUtils>>>updateSysUserInfo>>>进行入库状态>>>:{}", flag);
            }
            return ShiroSecurityUtils.sysUser;
        }
    }

    /* *
     * @description: 清除用全局用户信息
     * @author xiaoCoder
     * @date: 2021/8/17 1:20
     * @param: []
     * @return: void
     */
    public void clearGlobalSysUserInfo() {
        if (ShiroSecurityUtils.sysUser != null) ShiroSecurityUtils.sysUser = null;
    }

    /* *
     * @description: 获取用户信息+id
     * @author xiaoCoder
     * @date: 2021/8/19 15:42
     * @param: []
     * @return: java.lang.String
     */
    public static String getSysUserIdAndUsername(){
        if (ShiroSecurityUtils.sysUser == null) return null;
        return ShiroSecurityUtils.sysUser.getUsername() + ShiroSecurityUtils.getLoginSysUserInfo().getId();
    }


}
